package com.example.optimizeapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.example.optimizeapplication.receive.OrderARecevieer;
import com.example.optimizeapplication.receive.OrderBRecevieer;
import com.example.optimizeapplication.receive.ShockReceiver;
import com.example.optimizeapplication.receive.StandarReceiveer;

public class MainActivity extends AppCompatActivity {

    private StandarReceiveer standarReceiveer;
    public static final String orderAction = "orderaction_main";
    private OrderARecevieer orderARecevieer;
    private OrderBRecevieer orderBRecevieer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //有序广播
        findViewById(R.id.btn_broadcast_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(orderAction);
                 //发送广播
                 sendOrderedBroadcast(intent,null);

            }
        });
        //无序标准广播
        findViewById(R.id.btn_broadcast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StandarReceiveer.standardAction);
                sendBroadcast(intent);
            }
        });

       findViewById(R.id.btn_broadcast_quietness).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String fullname = "com.example.optimizeapplication.receive.ShockReceiver";
               Intent intent = new Intent(ShockReceiver.action);
               ComponentName componentName = new ComponentName(MainActivity.this,fullname);
               intent.setComponent(componentName);
               sendBroadcast(intent);
           }
       });

    }

    @Override
    protected void onStart() {
        super.onStart();

        standarReceiveer = new StandarReceiveer();
        //创建一个意图过滤器，只处理standardAction的广播
        IntentFilter intentFilter = new IntentFilter(StandarReceiveer.standardAction);
        //注册接收一个接收器，注册之后才能正常接收广播
        registerReceiver(standarReceiveer,intentFilter);


        //有序广播注册
        orderARecevieer = new OrderARecevieer();
        IntentFilter intentFilterA = new IntentFilter(orderAction);
        intentFilterA.setPriority(8);
        registerReceiver(orderARecevieer,intentFilterA);


        orderBRecevieer = new OrderBRecevieer();
        IntentFilter intentFilterB = new IntentFilter(orderAction);
        intentFilterB.setPriority(10);
        registerReceiver(orderBRecevieer,intentFilterB);




    }

    @Override
    protected void onStop() {
        super.onStop();
        //注销接收器，注销之后不再接收广播
        unregisterReceiver(standarReceiveer);
        unregisterReceiver(orderARecevieer);
        unregisterReceiver(orderBRecevieer);
    }

}